package com.stedu.controller;

import com.stedu.bean.RepairTask;
import com.stedu.bean.Repairer;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.AdminService;
import com.stedu.service.RepairTaskService;
import com.stedu.service.RepairerService;
import com.stedu.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/repairer")
@Validated
public class PepairerUserController {
    @Autowired
    private RepairTaskService repairTaskService;
    @Autowired
    private RepairerService repairerService;
    @Autowired
    private AdminService adminService;

    //查询当前员工所属的任务
    @GetMapping("/{rid}")
    public RespBean selectTaskByRid(@PathVariable("rid") Integer rid) throws SteduException {
        List<RepairTask> repairtaskList = repairTaskService.selectTaskByRid(rid);
        return RespBean.ok("", repairtaskList);
    }
    //修改个人信息
    @PutMapping("/repairerInfo")
    public RespBean updateUser(@Validated @RequestBody Repairer repairer) {
        if (repairerService.update(repairer)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败");
        }
    }
    //获取用户名
    @GetMapping("getRepairmenInfo")
    public RespBean getUsername(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        //解析jwt
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");
        //根据id查询用户信息
        Repairer repairer = repairerService.selectByRid(id);
        return RespBean.ok("", repairer);
    }
}
